// http.js
// 通常可以吧 baseUrl 单独放在一个 js 文件了
var baseUrl ;

//测试环境
if (process.env.NODE_ENV === 'development') {
 // Vue.prototype.serverUrl = "http://localhost:9007/api";
	baseUrl = 'http://120.237.35.182:8088/'
}
//正式环境
if (process.env.NODE_ENV === 'production') {
 //Vue.prototype.serverUrl = "http://120.237.35.126:8079/api";
	baseUrl = 'http://192.168.8.19:8088/'
	//baseUrl = 'http://120.237.35.182:8088/'
}

const uploadUrl = 'file/upload'
// 通常可以吧 baseUrl 单独放在一个 js 文件了
//const baseUrl = 'http://192.168.199.128:8088/'

 const request = (options = {}) => {
 // 在这里可以对请求头进行一些设置
 // 例如：
	//  options.header = {
	// 		"Content-Type": "application/x-www-form-urlencoded"
	// }
	return new Promise((resolve, reject) => {
		uni.showLoading({
		    title: '加载中'
		});
		//console.log(options.data)
		//console.log(uni.getStorageSync('token'))
		uni.request({
			url: baseUrl + options.url || '',
			method: options.type || 'GET',
			data: options.data || {},
			header: options.header || {'token' : uni.getStorageSync('token')}
		}).then(data => {
			let [err, res] = data; 
			uni.hideLoading();
			resolve(res);
		}).catch(error => {
			uni.hideLoading();
			reject(error)
		})
	});
}

 const upload = (filePath) => {
 // 在这里可以对请求头进行一些设置
 // 例如：
	//  options.header = {
	// 		"Content-Type": "application/x-www-form-urlencoded"
	// }
	return new Promise((resolve, reject) => {
		uni.showLoading({
		    title: '加载中'
		});
		uni.uploadFile({
			url: baseUrl + uploadUrl,
			name: 'file',
			filePath: filePath 
		}).then(data => {
			let [err, res] = data; 
			uni.hideLoading();
			resolve(res);
		}).catch(error => {
			uni.hideLoading();
			reject(error)
		})
	});
}

 const get = (url, data, options = {}) => {
	options.type = 'GET';
	options.data = data;
	options.url = url;
	return request(options)
}

const post = (url, data, options = {}) => {
	options.type = 'POST';
	options.data = data;
	options.url = url;
	return request(options)
}


export default {
	baseUrl,
	request,
	get,
	post,
	upload
}